<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Product;
use App\Product_type;

class ProductController extends Controller
{
    /**
     * 获取产品列表
     */
    public function getProductList()
    {
        $list = Product::where('show_order','>','0')->where('type_id',1)->orderBy('show_order','asc')->get()->toArray();

        $higt = Product::where('show_order','>','0')->where('type_id',2)->orderBy('show_order','asc')->get()->toArray();

        $list = self::Info($list);
        $higt = self::Info($higt);

        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'list'=>$list,
                'higt'=>$higt
            )
        );

        return response()->json($return);

    }


    static  function Info($ret)
    {
        $arr = [];
        foreach($ret as &$value )
        {
            $arr[] .= $value['name'];
            $value['desction'] = explode(';',$value['desc']);
            unset($value['name']);
            unset($value['desc']);
        }

        return $ret;
    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 获取产品商品详情
     */
    public function  getProductInfo(Request $request)
    {
        if (empty($request->input('pay_id'))){
            $return = array(
                'code' => -1,
                'msg' => "产品ID不能为空",
                'data' => array()
            );
            return response()->json($return);
        }

        $product_id = $request->input('pay_id');

        $info = Product::find($product_id)->toArray();

        if(!empty($info))
        {
            $product = Product_type::find($info['type_id']);
        }
        $info['type_name'] = $product->type_name;

        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'info'=>$info
            )
        );

        return response()->json($return);

    }


}